const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()
const userModel = db.collection('pk_user')

exports.main = async () => {
  const { OPENID: openid } = cloud.getWXContext()
  const { list } = await userModel
    .aggregate()
    .match({ _openid: openid })
    .limit(1)
    .end()
  if (list.length === 0) { return null }
  const userInfo = {
    ...list[0]
  }
  return userInfo
}
